package jinyilw.swing.rlaf.ui;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.graphics.ShapeTools;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JButton;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

/**
 * @author Jinyi
 */
public class RArrowButton extends JButton
{
	private static final long serialVersionUID = 5795094451409929034L;
	private static float defautAlpha = 0.35f, blackThemeAlpha = 0.2f;
	private static Color graColor1 = new Color(200, 200, 200);
	private static Color graColor2 = Color.white;
	private int direction;

	private GradientPaint backPaint;
	private RoundRectangle2D.Float roundRect;
	private Shape triangle;
	private float triangleWidth, triangleHeight;
	private int arc = 1;

	private final RenderingConfig renderingConfig = new RenderingConfig();

	public RArrowButton(int direction)
	{
		super();
		setRequestFocusEnabled(false);
		setOpaque(false);
		this.direction = direction;
	}

	public int getDirection()
	{
		return direction;
	}

	public void setDirection(int direction)
	{
		this.direction = direction;
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		boolean unResized = width == getWidth() && height == getHeight();
		super.setBounds(x, y, width, height);
		if (unResized)
			return;
		roundRect = new RoundRectangle2D.Float(1, 1, width - 2, height - 2, arc,
				arc);
		backPaint = new GradientPaint(0, 0, graColor1, 0, height, graColor2);
		float size = Math.min(width / 4f, height / 4f);
		if (direction == NORTH || direction == SOUTH)
		{
			triangleWidth = size * 2;
			triangleHeight = size;
		} else
		{
			triangleWidth = size;
			triangleHeight = size * 2;
		}
		triangle = ShapeTools.createTriangle(direction, triangleWidth,
				triangleHeight);
	}
	@Override
	public void paint(Graphics g)
	{
		boolean isPressed = getModel().isPressed();
		boolean isEnabled = isEnabled();
		int h = getHeight(), w = getWidth();

		Graphics2D g2d = (Graphics2D) g;
		float alpha = UITools.isBlack() ? blackThemeAlpha : defautAlpha;
		Composite composite = ComponentTools.applyEnableAlpha(g2d, isEnabled,
				alpha, 0.15f);
		renderingConfig.setAAPainting(g2d);
		g2d.setPaint(backPaint);
		g2d.fill(roundRect);
		g2d.setColor(RThemeColor.colorSet.borderColor);
		g2d.draw(roundRect);

		if (isEnabled)
			g2d.setComposite(composite);
		if (isEnabled && isPressed)
			g2d.translate(0.8, 0.8);
		float x = (w - triangleWidth) / 2;
		float y = (h - triangleHeight) / 2;
		if (direction == WEST)
			x--;
		else if (direction == EAST)
			x++;
		g2d.translate(x, y);
		g2d.setColor(RThemeColor.colorSet.textColor);
		g2d.fill(triangle);
		g2d.translate(-x, -y);
		if (isEnabled && isPressed)
			g2d.translate(-0.8, -0.8);

		renderingConfig.resetAAPainting(g2d);
		if (!isEnabled)
			g2d.setComposite(composite);
	}

	@Override
	public void updateUI()
	{
		setUI(new BasicButtonUI());
	}

	@Override
	public Dimension getPreferredSize()
	{
		return new Dimension(16, 16);
	}

	@Override
	public Dimension getMinimumSize()
	{
		return new Dimension(5, 5);
	}

	@Override
	public Dimension getMaximumSize()
	{
		return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
	}

	@Override
	public boolean isFocusTraversable()
	{
		return false;
	}
}
